"""卖家会话表定义"""

import datetime
from sqlalchemy import Column, String, DateTime, Integer, Text, BigInteger
from sqlalchemy.sql import func, text

from config.database import Base


class SellerConversation(Base):
    """卖家会话表 - 用于管理chat_id与seller_id、item_id的映射关系"""

    __tablename__ = "seller_conversation"

    # 主键（使用BigInteger，与迁移脚本一致）
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="主键ID")
    
    # 会话标识
    conversation_id = Column(String(128), nullable=False, unique=True, index=True, comment="会话ID（sellerId#itemId或chat_id）")
    chat_id = Column(String(128), nullable=True, unique=True, index=True, comment="聊天ID（官方chat_id，如55138335758）")
    
    # 客户端信息
    client_id = Column(String(100), nullable=False, index=True, comment="客户端ID")
    
    # 卖家信息
    seller_id = Column(String(100), nullable=False, index=True, comment="卖家ID")
    seller_name = Column(String(200), nullable=True, comment="卖家昵称")
    seller_avatar = Column(String(500), nullable=True, comment="卖家头像URL")
    
    # 商品信息
    item_id = Column(String(100), nullable=True, index=True, comment="商品ID")
    item_title = Column(String(500), nullable=True, comment="商品标题")
    item_image = Column(String(500), nullable=True, comment="商品主图URL")
    item_price = Column(String(50), nullable=True, comment="商品价格")
    item_tags = Column(Text, nullable=True, comment="商品标签（JSON数组）")
    item_images = Column(Text, nullable=True, comment="商品图片列表（JSON数组）")
    video_info = Column(Text, nullable=True, comment="商品视频信息（JSON对象）")
    item_status = Column(Integer, nullable=True, comment="商品状态（0=在线，1=已卖，-2=已删除）")
    item_status_str = Column(String(50), nullable=True, comment="商品状态文本")
    item_info_updated_at = Column(DateTime, nullable=True, comment="商品信息最后更新时间")
    
    # 会话状态
    unread_count = Column(Integer, nullable=False, default=0, comment="未读消息数")
    last_message = Column(Text, nullable=True, comment="最后一条消息内容")
    last_message_time = Column(DateTime, nullable=True, comment="最后消息时间")
    last_message_direction = Column(String(10), nullable=True, comment="最后消息方向（send/recv）")
    
    # 会话元数据
    conversation_type = Column(Integer, nullable=False, default=1, comment="会话类型（1=单聊）")
    
    # 时间戳（与迁移脚本一致）
    created_at = Column(DateTime, nullable=False, default=datetime.datetime.now, server_default=func.now(), comment="会话创建时间")
    updated_at = Column(DateTime, nullable=False, default=datetime.datetime.now, onupdate=datetime.datetime.now, server_default=func.now(), comment="会话更新时间")
    
    # BaseMixin 兼容字段
    create_time = Column(DateTime, nullable=True, comment="创建时间")
    update_time = Column(DateTime, nullable=True, comment="更新时间")
    del_flag = Column(String(1), nullable=False, default='0', server_default=text("'0'"), comment="删除标志（0代表存在 1代表删除）")

